#include <ch9.h>
#include <Max3421e.h>
#include <Max3421e_constants.h>
#include <Max_LCD.h>
#include <Usb.h>

#include <AndroidAccessory.h>

AndroidAccessory acc("Manufacturer",
"Model",
"Description",
"1.0",
"edu.uidaho.pong",
"0000000012345678");

/* hook up potentiometer edges to 5v and ground. 
 Wiper arm to Analog in pin A0
 
 Should be good from there
 
 */
int DelayValue = 500;
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorPin2 = A2;
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0; 

void setup() {
  // declare the ledPin as an OUTPUT:

  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(38, OUTPUT);
 
  acc.powerOn();
}

void loop()
{
   digitalWrite(38,HIGH);
  byte msgStart[1]; // one byte used for values
  byte msg[3]; // one byte used for markers
  byte msgIn[1];

  sensorValue = analogRead(sensorPin);
  sensorValue2 = analogRead(sensorPin2);
  sensorValue = map(sensorValue, 0, 1023, 1, 125);
  sensorValue2 = map(sensorValue2, 0, 1023, 1, 125);

  if (acc.isConnected())
  {
    //send null character to initialize
//  msgStart[0] = 0;
    //    acc.write(msgStart,1);
    //    delay(DelayValue);
    //send Data from first pot
    msg[0] = sensorValue;
    //    acc.write(msg, 1);
    //send Data from second pot
    //    delay(DelayValue);
    msg[1] = sensorValue2;
    acc.write(msg, 2);
    //    delay(DelayValue);


    int len = acc.read(msgIn, sizeof(msgIn), 1); // read data into msg variable
    if (len > 0) {
      if (msgIn[0] == 1){ // compare received data
        Serial.print("try to turn on");
        digitalWrite(ledPin,HIGH); // turn on light
      }
      else{
        digitalWrite(ledPin,LOW); // turn off light
        Serial.print("try to turn on");
      }
    }

    //Serial.println(sensorValue, HEX);

    //delay(50); 

  }
}










